package icu.wdtt.config;

import icu.wdtt.Interceptors.AuthorityInterceptor;
import icu.wdtt.Interceptors.CheckCodeInterceptor;
import icu.wdtt.Interceptors.CrossInterceptor;
import icu.wdtt.Interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.*;

/**
 * 对于SpringMvc的配置
 */
@Configuration
public class WebAppConfig implements WebMvcConfigurer {
    @Autowired
   private LoginInterceptor loginInterceptor;
    @Autowired
    private CheckCodeInterceptor checkCodeInterceptor;
    @Autowired
    private AuthorityInterceptor authorityInterceptor;
    @Autowired
    private CrossInterceptor crossInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //放行接口
        String[] exclude = new String[]{"/user/login","/user/register","/user/checkCode","/user/isUserName","/user/quit",
                                        "/user/main","/user/email","/user/isEmail","/user/retPassword"};
        //拦截接口
        String[] pathPatterns = new String[]{"/user/**","/role/**","/files/**","/authority/**","/admin/**"};
        /*
        解决跨域问题的拦截器
         */
        registry.addInterceptor(crossInterceptor).addPathPatterns("/**");
       /*
       设置一个登陆验证拦截器
        */
        registry.addInterceptor(loginInterceptor).addPathPatterns(pathPatterns).excludePathPatterns(exclude);
        /*
        设置验证码的验证
         */
        registry.addInterceptor(checkCodeInterceptor).addPathPatterns("/user/register");
        /*
        设置权限拦截器
         */
         registry.addInterceptor(authorityInterceptor).addPathPatterns(pathPatterns).excludePathPatterns(exclude);
    }

    /**
     * 设置静态资源放行
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

        // 解决swagger无法访问
        registry.addResourceHandler(new String[]{"/swagger-ui.html","/doc.html"}).addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/").addResourceLocations("classpath:/META-INF/resources/webjars/");


    }

    /**
     * 设置首页,以及一些特殊的资源放行
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");

        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        WebMvcConfigurer.super.addViewControllers(registry);
    }

    /**
     * 解决ajax的跨域问题
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许携带的请求头
        String[] headers = new String[]{"checkId","emailCode"," Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With"};
        //设置允许获取的响应头
        String[] eheaders = new String[]{"checkId","emailCode"};
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(false)
                .allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
                //设置请求头的允许
                .allowedHeaders(headers)
                //设置响应头的获取
                .exposedHeaders(eheaders)
                .maxAge(3600);

    }



}
